import echarts from 'echarts'
import testApi from '@/api/testApi'

export default {
  name: 'eventResponse',
  data() {
    return {
      abnormalInfoList: [],
      chartsDatas: []
    }
  },
  created() {
  },

  async mounted() {
    await this.getList()
    this.makeCharts()
  },
  methods: {
    async getList() {
      await testApi.getAbnormalInfoList().then((res) => {
        this.abnormalInfoList = res.data
      }).catch((error) => {
        this.abnormalInfoList = [
          {
            id: '0',
            hostId: '12',
            supplierName: '阿里云',
            abnormalType: '0',
            abnormalTime: '2023-07-02 15:42:53',
            responseTime: '2023-07-02 15:43:12',
            recoverTime: '2023-07-02 15:45:33'
          },
          {
            id: '1',
            hostId: '22',
            supplierName: '阿里云',
            abnormalType: '1',
            abnormalTime: '2023-07-02 15:42:23',
            responseTime: '2023-07-02 15:44:11',
            recoverTime: '2023-07-02 15:45:12'
          },
          {
            id: '2',
            hostId: '17',
            supplierName: '联通云',
            abnormalType: '0',
            abnormalTime: '2023-07-02 15:12:12',
            responseTime: '2023-07-02 15:12:31',
            recoverTime: '2023-07-02 15:16:08'
          },
          {
            id: '3',
            hostId: '14',
            supplierName: '华为云',
            abnormalType: '1',
            abnormalTime: '2023-07-02 14:29:14',
            responseTime: '2023-07-02 14:29:57',
            recoverTime: '2023-07-02 14:33:43'
          },
          {
            id: '4',
            hostId: '19',
            supplierName: '华为云',
            abnormalType: '2',
            abnormalTime: '2023-07-02 14:22:33',
            responseTime: '2023-07-02 14:22:42',
            recoverTime: '2023-07-02 14:25:55'
          },
          {
            id: '5',
            hostId: '33',
            supplierName: '火山云',
            abnormalType: '3',
            abnormalTime: '2023-07-02 13:42:33',
            responseTime: '2023-07-02 13:43:01',
            recoverTime: '2023-07-02 13:44:17'
          },
          {
            id: '6',
            hostId: '11',
            supplierName: '火山云',
            abnormalType: '2',
            abnormalTime: '2023-07-02 13:22:33',
            responseTime: '2023-07-02 13:22:13',
            recoverTime: '2023-07-02 13:23:33'
          }
        ]
      })

      await testApi.getEventResponseChartData().then((res) => {
        this.chartsDatas = res.data
      }).catch((error) => {
        this.chartsDatas = [
          { id: 0, supplierName: '阿里云', abnormalNum: 12, argResponseTime: 12, argRecoverTime: 49 },
          { id: 1, supplierName: '联通云', abnormalNum: 15, argResponseTime: 9, argRecoverTime: 35 },
          { id: 2, supplierName: '华为云', abnormalNum: 8, argResponseTime: 15, argRecoverTime: 79 },
          { id: 3, supplierName: '火山云', abnormalNum: 11, argResponseTime: 21, argRecoverTime: 125 }
        ]
      })
    },
    makeCharts() {
      this.makePieChart()
      this.makeBarCharts()
    },
    fullScreen() {
      const element = document.getElementById('entirety')
      if (element.requestFullscreen) {
        element.requestFullscreen()
      } else if (element.mozRequestFullScreen) {
        element.mozRequestFullScreen()
      } else if (element.webkitRequestFullscreen) {
        element.webkitRequestFullscreen()
      } else if (element.msRequestFullscreen) {
        element.msRequestFullscreen()
      }
    },
    getTypeText(type) {
      switch (type) {
        case '0':
          return {
            label: '网络异常'
          }
        case '1':
          return {
            label: '传输异常'
          }
        case '2':
          return {
            label: '服务失败'
          }
        case '3':
          return {
            label: '计算错误'
          }
      }
    },
    makePieChart() {
      const pieChart = echarts.init(document.getElementById('pieChart'), 'light')
      const data = []
      for (let i = 0; i < this.chartsDatas.length; i++) {
        data.push({
          value: this.chartsDatas[i].abnormalNum,
          name: this.chartsDatas[i].supplierName
        })
      }
      const option = {
        tooltip: {
          trigger: 'item',
          formatter: '{b} : {c} ({d}%)'
        },
        legend: {
          orient: 'horizontal',
          left: 'left',
          textStyle: {
            color: '#FFFFFF'
          }
        },
        series: [
          {
            name: '',
            type: 'pie',
            radius: '50%',
            data: data,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            },
            label: {
              formatter: '{b}: {c} ({d}%)'
            }
          }
        ]
      }
      pieChart.setOption(option)
    },
    makeBarCharts() {
      const responseTimeChart = echarts.init(document.getElementById('responseTimeChart'), 'light')
      const recoverTimeChart = echarts.init(document.getElementById('recoverTimeChart'), 'light')

      const data = this.chartsDatas
      const responseTimeOption = {
        tooltip: {
          trigger: 'axis',
          formatter(params) {
            return params[0].name
          },
          axisPointer: {
            type: 'shadow'
          }
        },
        yAxis: {
          type: 'value',
          show: true,
          axisLabel: {
            color: 'rgb(255, 255, 255, 0.5)',
            show: true
          }
        },
        xAxis: {
          type: 'category',
          data: data.map(s => s.supplierName),
          axisLabel: {
            color: 'rgb(255, 255, 255, 0.5)',
            formatter(val) {
              if (val.length < 5) {
                return val
              } else {
                return val.substring(0, 4) + '..'
              }
            },
            show: true
          }
        },
        series: [{
          data: data.map(s => s.argResponseTime),
          type: 'bar',
          barWidth: 25,
          itemStyle: {
            normal: {
              label: {
                show: true, // 开启显示
                position: 'top',
                textStyle: { // 数值样式
                  color: '#5FA4E9',
                  fontSize: 12
                }
              },
              color: '#307CE0'
            }
          }
        }]
      }
      const recoverTimeOption = {
        tooltip: {
          trigger: 'axis',
          formatter(params) {
            return params[0].name
          },
          axisPointer: {
            type: 'shadow'
          }
        },
        yAxis: {
          type: 'value',
          show: true,
          axisLabel: {
            color: 'rgb(255, 255, 255, 0.5)',
            show: true
          }
        },
        xAxis: {
          type: 'category',
          data: data.map(s => s.supplierName),
          axisLabel: {
            color: 'rgb(255, 255, 255, 0.5)',
            formatter(val) {
              if (val.length < 5) {
                return val
              } else {
                return val.substring(0, 4) + '..'
              }
            },
            show: true
          }
        },
        series: [{
          data: data.map(s => s.argRecoverTime),
          type: 'bar',
          barWidth: 25,
          itemStyle: {
            normal: {
              label: {
                show: true, // 开启显示
                position: 'top',
                textStyle: { // 数值样式
                  color: '#5FA4E9',
                  fontSize: 12
                }
              },
              color: '#307CE0'
            }
          }
        }]
      }
      responseTimeChart.setOption(responseTimeOption)
      recoverTimeChart.setOption(recoverTimeOption)
    }

  }
}
